import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-06
 * Time: 23:41
 */
public class MyArrayList implements MyList{
    private int[] arr;
    private int size;

    private static final int DEFAULT_CAPACITY = 10;
    //构造方法
    public MyArrayList() {
        this.arr = new int[DEFAULT_CAPACITY];
    }
    public MyArrayList(int capacity) {
        this.arr = new int[capacity];
    }

    //新增元素_尾插
    @Override
    public void add(int data) {
        //判断是否满了没有
        if (this.size == this.arr.length) {
            //满了就进行扩容
            capacityExpansion();
        }
        this.arr[this.size++] = data;
    }

    //扩容方法
    private void capacityExpansion() {
        this.arr = Arrays.copyOf(this.arr,2*this.arr.length);
    }

    //在指定位置插入
    @Override
    public void add(int pos, int data) {
        //查找前判断索引是否合法
        judgeLegality(pos);
        //判满
        if (this.size == this.arr.length) {
            //满了就进行扩容
            capacityExpansion();
        }

        //判断是否为尾插
        if (pos == this.size) {
            arr[this.size++] = data;
            return;
        }
        //如果不是尾插，就需要把pos位置及其后面的数据向后移
        for (int i = this.size-1; i >= pos; i--) {
            this.arr[i+1] = this.arr[i];
        }
        this.arr[pos] = data;
        this.size++;
    }
    //判断索引是否合法1
    private void judgeLegality(int pos) {
        if (pos < 0 || pos > this.size) {
            throw new IndexIllegal("索引不合法！");
        }
    }

    //检测顺序表是否包含某个数据
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (this.arr[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个数据在顺序表中的索引
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (this.arr[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //获取指定索引的数据
    @Override
    public int get(int pos) {
        //判断是否为空
        isEmpty();
        //判断索引是否合法
        judgeLegality2(pos);
        return this.arr[pos];
    }
    //判断是否为空
    private void isEmpty() {
        if (this.size == 0) {
            throw new EmptyException("顺序表为空！");
        }
    }
    //判断索引是否合法2
    private void judgeLegality2(int pos){
        if (pos < 0 || pos > this.size) {
            throw new IndexIllegal("索引不合法！");
        }
    }

    //修改指定索引的数据
    @Override
    public void set(int pos, int value) {
        //判断是否为空
        isEmpty();
        //判断索引是否合法
        judgeLegality2(pos);
        this.arr[pos] = value;
    }

    //删除指定的数据
    @Override
    public void remove(int toRemove) {
        //判断是否为空
        isEmpty();
        //获取该数据的地址
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("顺序表中没有这个数据！");
            return;
        }

        //删除的原理：用后面的数据覆盖前面的
        for (int i = index; i < this.size-1; i++) {
            this.arr[i] = this.arr[i+1];
        }
        this.size--;
    }

    //获取顺序表当前长度
    @Override
    public int size() {
        return this.size;
    }

    //清空顺序表
    @Override
    public void clear() {
        this.size = 0;
    }

    //打印顺序表
    @Override
    public void display() {
        for (int i = 0; i < this.size; i++) {
            System.out.print(this.arr[i] + " ");
        }
        System.out.println();
    }
}
